import util from './utils.js'
import * as userApi from '@/api/user'
import config from '@/config'
let miniList = ['3.3','3.8','3.6','3.5','3.2','3.0']
let miniStr = ""
let miniUrl = ""
// 多端进入客服组合方法
async function enterIntoservier(url,user_id){
	let _this = this;
	let userId = uni.getStorageSync("userInfo")?uni.getStorageSync("userInfo").user_id:"未登录"
	let linkList = await checkConnection()
	let linkUrl = await serviceMatch(linkList)
	// #ifdef H5 
	location.href = linkUrl +'?u_cust_id=9'+"&u_cust_name="+"ID:"+userId
	// #endif
	// #ifdef MP-WEIXIN 
	// if(miniList.indexOf(miniStr) != -1 && miniUrl){
	// 	wx.openCustomerServiceChat({
	// 	  extInfo: {url: linkUrl},
	// 	  corpId: 'wwed55a448dd8f7a6a',
	// 	  success(res) {}
	// 	})
	// }else{
		uni.setStorageSync("miniServe",linkUrl)
		uni.navigateTo({
			url:"/pages/user/server?userId=" + userId 
		})
	// }
	// #endif
	// #ifdef MP-BAIDU || MP-TOUTIAO || MP-KUAISHOU || MP-ALIPAY || APP-PLUS
	uni.setStorageSync("miniServe",linkUrl)
	uni.navigateTo({
		url:'/pages/user/server'
	})
	// #endif
}
function serviceMatch(data){
	return new Promise((resolve,reject) => {
		// #ifdef H5
		if(util.checkBrowser()){
			if(util.checkisMobile()){
				resolve(data.linkWx?data.linkWx:util.checkserviceUrl(data.link))
			}else{
				resolve(data.linkH5?util.checkserviceUrl(data.linkH5):util.checkserviceUrl(data.link))
			}
		}else{
			resolve(data.linkH5?util.checkserviceUrl(data.linkH5):util.checkserviceUrl(data.link))
		}
		// #endif
		// #ifdef MP-WEIXIN
		let appid = uni.getAccountInfoSync().miniProgram.appId;
		userApi.getstatus({
				appid,
			})
			.then(result => {
				if(result.code === 1){
					miniStr = result.data[0].mini_no
					if(miniList.indexOf(result.data[0].mini_no) != -1){
						if(data.linkMini){
							miniUrl = true
							resolve(data.linkMini)
						}else{
							miniUrl = false
							resolve(data.link)
						}
					}else{
						resolve(data.linkH5?data.linkH5:data.link)
					}
				}
			})
		// #endif
		// #ifdef MP-BAIDU
		resolve(data.linkBd?data.linkBd:data.link)
		// #endif
		// #ifdef MP-KUAISHOU
		resolve(data.linkKs?data.linkKs:data.link)
		// #endif
		// #ifdef MP-TOUTIAO
		resolve(data.linkDy?data.linkDy:data.link)
		// #endif
		// #ifdef MP-ALIPAY
		resolve(data.linkAli?data.linkAli:data.link)
		// #endif
		// #ifdef APP-PLUS
		resolve(data.linkApp?data.linkApp:data.link)
		// #endif
	})

}
function checkConnection(){
	return new Promise((resolve,reject) => {
		userApi.connection({
				wxapp_id: config.wxapp_id,
			})
			.then(result => {
				if(result.code === 1){
					console.log(result);
					resolve(result.data)
				}
			})
	})
}

module.exports = {
	enterIntoservier:enterIntoservier
}